import type {Panel} from "@/business/entity/panel/panel.entity";
import type {PanelService} from "@/business/interface/panel.interface";

/**
   *  绘制面板实现类
   * @author JiangXincan
   * @date 2023/01/29 14:39
   */
export class PanelServiceImpl implements PanelService {

     /**
      *  创建画布，返回画布上下文实例
      * @author JiangXincan
      * @date 2023/01/28 18:23
      * @return CanvasRenderingContext2D
      */
    create(panel: Panel):  CanvasRenderingContext2D {
        const element = document.getElementById(panel.id);
        if (!element || !(element instanceof HTMLCanvasElement)) {
            throw new Error('获取画布容器失败')
        }
         const context = element.getContext("2d");
         if (!context || !(context instanceof CanvasRenderingContext2D)) {
             throw new Error('2D画布渲染失败')
         }
         element.width =  panel.width;
         element.height = panel.height;
         element.style.backgroundColor = panel.backgroundColor;
         return context;
    }

}